import { createRouter, createWebHistory,createWebHashHistory, RouteRecordRaw } from 'vue-router'
import store from '../store/index'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/Index.vue'),
    children: [
      {
        path: '/test',
        name: 'test',
        meta:{
          id:1,
          keepAlive:true
        },
        component: () => import('../views/test/Test.vue'),
        
      },
      {
        path: '/test',
        name: 'test',
        meta:{
          id:1,
          keepAlive:true
        },
        component: () => import('../views/test/Test.vue')
      },
      {
        path: '/exam',
        name: 'exam',
        meta:{
          id:2
        },
        component: () => import('../views/exam/Exam.vue')
      },
      {
        path: '/subjects',
        name: 'subjects',
        meta:{
          id:3
        },
        component: () => import('../views/subjects/Subjects.vue')
      },
      {
        path: '/subjectsAdd/:id',
        name: 'subjectsAdd',
        meta:{
          id:36
        },
        component: () => import('../views/subjects/SubjectsAdd.vue')
      },
      {
        path: '/TestAdd/:id',
        name: 'testAdd',
        meta:{
          id:30
        },
        component: () => import('../views/test/TestAdd.vue')

      },
      {

        path: '/teacher',
        name: 'teacher',
        meta:{
          id:6
        },
        component: () => import('../views/teacher/TeaCher.vue')
      },
      {
        path: '/department',
        name: 'department',
        meta:{
          id:5
        },
        component: () => import('../views/department/DepartMent.vue')
      },
      {
        path: '/role',
        name: 'role',
        meta:{
          id:10
        },
        component: () => import('../views/role/Role.vue')
      },
      {
        path: "/set",
        name: "set",
        meta:{
          id:2
        },
        component: () => import('../views/setup/SetUp.vue')
      },
      {
        path: '/databaselist',
        name: 'databaselist',
        meta:{
          id:4
        },
        component: () => import('../views/Databaselist/Databaselist.vue')
      },
      {
        path: '/databasequestionlist',
        name: 'databasequestionlist',
        meta:{
          id:37
        },
        component: () => import('../views/Databaselist/DatabasequestionList.vue')
      },
      {

        path: '/examstudent',
        name: 'examstudent',
        meta:{
          id:37
        },
        component: () => import('../views/exam/ExamStudent.vue')

      },
      {
        path: '/stutest',
        name: 'stutest',
        meta:{
          id:1
        },
        component: () => import('../views/Stutest/StutestList.vue')
      },
      {
        path: '/student',
        name: 'student',
        meta:{
          id:8
        },
        component: () => import('../views/students/Student.vue')
      },
      {
        path: '/analyse',
        name: 'analyse',
        meta:{
          id:29
        },
        component: () => import('../views/test/Analyse.vue')
      },
      {
        path: '/classes',
        name: 'classes',
        meta:{
          id:7
        },
        component: () => import('../views/Classes/Classes.vue')
      },
    ]
  },
  {
    path: '/examresults',
    name: 'examresults',
    meta:{
      id:1
    },
    component: () => import('../views/Stutest/ExamResults.vue')
  },
  {
    path: '/examprepare',
    name: 'examprepare',
    meta:{
      id:1
    },
    component: () => import('../views/Stutest/ExamPrepare.vue')
  },
  {
    path: '/stuexamwrong',
    name: 'stuexamwrong',
    meta:{
      id:1
    },
    component: () => import('../views/Stutest/StuExamWrong.vue')
  },
  {
    path: '/stuexam',
    name: 'stuexam',
    meta:{
      id:1
    },
    component: () => import('../views/Stutest/StuExam.vue')
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
router.beforeEach((to:any, from:any, next:any) => {
  const isLogin = sessionStorage.getItem('token') ? true : false;
  if (to.path == '/') {
    next();
  }else if(!isLogin){
    next('/');
  }else {
    // 查找当前要去的路径是否和该角色的权限符合 如果有放行 否则拦截
    const pag = store.state.DataList.find((item:any)=>(item.id==to.meta.id))
    if(!pag){
      next('/')
    }else{
      next()
    }
  }
});

export default router